{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "41ac89d0-109e-46ba-9103-e38758776939",
   "metadata": {},
   "source": [
    "## How to save snapshots faster?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "404dd0f2-a2d8-4ef7-a42f-a79d332185fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "from sklearn import datasets\n",
    "\n",
    "from evidently import ColumnMapping\n",
    "from evidently.report import Report\n",
    "from evidently.metric_preset import DataDriftPreset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f5642b9a-ced8-4128-8714-b6f39803d3bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/homebrew/Caskroom/miniconda/base/envs/py11/lib/python3.11/site-packages/sklearn/datasets/_openml.py:1002: FutureWarning: The default value of `parser` will change from `'liac-arff'` to `'auto'` in 1.4. You can set `parser='auto'` to silence this warning. Therefore, an `ImportError` will be raised from 1.4 if the dataset is dense and pandas is not installed. Note that the pandas parser may return different data types. See the Notes Section in fetch_openml's API doc for details.\n",
      "  warn(\n"
     ]
    }
   ],
   "source": [
    "#Dataset for Data Drift calculation\n",
    "adult_data = datasets.fetch_openml(name='adult', version=2, as_frame=True)\n",
    "adult = adult_data.frame\n",
    "\n",
    "adult_ref = adult[~adult.education.isin(['Some-college', 'HS-grad', 'Bachelors'])]\n",
    "adult_cur = adult[adult.education.isin(['Some-college', 'HS-grad', 'Bachelors'])]\n",
    "\n",
    "adult_cur.iloc[:2000, 3:5] = np.nan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7a694c67-5e0b-43af-ac9a-dacb16d095ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_drift_report = Report(metrics=[\n",
    "    DataDriftPreset(),\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2734f1a6-af65-49ce-983d-298f5435da31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "278 ms ± 13 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "data_drift_report.run(reference_data=adult_ref, current_data=adult_cur)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dc4887c3-b9e7-4632-999c-b6eedfd86f51",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.96 ms ± 172 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "data_drift_report.save('timeit_test.json')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f0d2a6e-9147-434f-a665-c7a96713760d",
   "metadata": {},
   "source": [
    "## EXPERIMENTAL: snapshot save optimization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1a4e5fd7-a4f8-4f27-ac6d-35a941caa5a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from evidently.suite import base_suite\n",
    "\n",
    "base_suite.USE_UJSON = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f547c055-fdeb-47aa-ac71-94dfe1f4eeeb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.86 ms ± 78.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "data_drift_report.save('timeit_test.json')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
